package cn.ncu.wechat.service.impl;

import cn.ncu.wechat.service.ThirdPartyApiClientService;
import cn.ncu.wechat.util.ThirdPartyApiClient;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.redis.core.RedisTemplate;
import org.springframework.stereotype.Service;

import java.util.HashMap;
import java.util.Map;
import java.util.concurrent.TimeUnit;

import static cn.ncu.wechat.util.ThirdPartyApiClient.addParamIfNotEmpty;

@Service
public class ThirdPartyApiClientServiceImpl implements ThirdPartyApiClientService {
    @Autowired
    private RedisTemplate redisTemplate;
    public String getToken(){
        String token = (String) redisTemplate.opsForValue().get("ThirdPartyToken");
        if (token==null){
            token = ThirdPartyApiClient.callThirdPartyApi();
            if (!token.startsWith("Error")){
                redisTemplate.opsForValue().set("ThirdPartyToken",token,24, TimeUnit.HOURS);
            } else token = "Get Token Error";
        }
        return token;
    }

    public  String getCardType(String mtyCode,String mtyName,String sort,Integer page,Integer size){
        String token = getToken();
        String response;
        System.out.println("token:"+token);
        if (!"Get Token Error".equals(token)){
            response = ThirdPartyApiClient.getCardType( mtyCode, mtyName, sort,String.valueOf(page),String.valueOf(size),token);
            if (response.contains("\"code\":401")){
                token = ThirdPartyApiClient.callThirdPartyApi();
                if (!token.startsWith("Error")){
                    response = ThirdPartyApiClient.getCardType( mtyCode, mtyName, sort,String.valueOf(page),String.valueOf(size),token);
                } else token = "Get Token Error";
                redisTemplate.opsForValue().set("ThirdPartyToken",token,24, TimeUnit.HOURS);
            }
        }
        else response = "error";
        System.out.println("response = "+response);
        return response;
    }

    public String getOrders(String ctpCtmcode,String ctpStatus,Integer currentPage,String endDate, String sort,Integer size){
        String token = getToken();
        String response = "error";
        if (!"Get Token Error".equals(token)){
            Map<String, String> queryParams = new HashMap<>();
            addParamIfNotEmpty(queryParams, "ctpCtmcode", ctpCtmcode);
            addParamIfNotEmpty(queryParams, "ctpStatus", ctpStatus);
            addParamIfNotEmpty(queryParams, "sort", sort);
            addParamIfNotEmpty(queryParams, "endDate", endDate);
            addParamIfNotEmpty(queryParams, "currentPage", String.valueOf(currentPage));
            addParamIfNotEmpty(queryParams, "size", String.valueOf(size));
            response = ThirdPartyApiClient.sendGetRequest(token,queryParams);
            if (response.contains("\"code\":401")){
                token = ThirdPartyApiClient.callThirdPartyApi();
                if (!token.startsWith("Error")){
                    response = ThirdPartyApiClient.sendGetRequest(token,queryParams);
                } else token = "Get Token Error";
                redisTemplate.opsForValue().set("ThirdPartyToken",token,24, TimeUnit.HOURS);
            }
        }
        return response;
    }


}
